package cn.tedu._05mvcboot02.controller;

import cn.tedu._05mvcboot02.commen.response.JsonResult;
import cn.tedu._05mvcboot02.mapper.UserMapper;
import cn.tedu._05mvcboot02.pojo.dto.AddUserDTO;
import cn.tedu._05mvcboot02.pojo.dto.UpdateUserDTO;
import cn.tedu._05mvcboot02.pojo.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 组合注解RestController, 包含Controller和ResponseBody
 */
@Api(tags = "用户模块")
@Slf4j
@Validated
@RestController
@RequestMapping("/v1/user")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    /**
     * 用户注册功能
     * 1.请求地址:/v1/user/insert
     * 2.请求方法:POST
     * 3.参数:username,password,nickname
     * 4.返回响应:String "用户添加完成!"
     */
    @ApiOperation(value = "1.添加用户")
    @PostMapping("/insert")
//    @ResponseBody
    public JsonResult insertUser(@RequestBody @Validated AddUserDTO addUserDTO){
        //打桩
        log.debug(addUserDTO.toString());
        //插入用户
        User user = new User();
        BeanUtils.copyProperties(addUserDTO,user);
        user.setCreated(new Date());
        //打桩
        log.debug(user.toString());
        userMapper.insertUser(user);
        return JsonResult.success();
    }

    /**
     * 查询所有用户功能
     * 1.请求地址:/v1/user/userList
     * 2.请求方法:GET
     * 3.参数:无
     * 4.返回响应:String "用户列表查询完成!"
     */
    @ApiOperation(value = "2.查询用户列表")
    @GetMapping("/userList")
//    @ResponseBody
    public JsonResult selectUserList(){
//        return userMapper.selectUserList().toString()+"\n"+"用户列表查询完成!";
        return JsonResult.success(userMapper.selectUserList());
    }

    /**
     * 删除指定用户功能
     * 1.请求地址:/v1/user/delete
     * 2.请求方法:GET
     * 3.参数:id
     * 4.返回响应:String "用户删除完成!"
     */
    @ApiOperation(value = "3.删除指定用户")
    @ApiImplicitParam(name = "id",value = "用户编号",required = true,
            example = "1",dataType = "int")
    @GetMapping("/delete")
//    @ResponseBody
    public JsonResult deleteUser(@RequestParam @Range(
            min = 100, max = 200, message = "用户编号范围:100-200") int id){
        //输入数据不合法
        if (id<0)
            throw new IllegalArgumentException("非法的id值");
        //删除用户
        userMapper.deleteUser(id);
        return JsonResult.success();
    }

    /**
     * 更新用户数据功能
     * 1.请求地址:/v1/user/update
     * 2.请求方法:POST
     * 3.参数:id,username,password,nickname,created
     * 4.返回响应:String "用户信息修改完成!"
     */
    @ApiOperation(value = "4.更新用户信息")
    @PostMapping("/update")
//    @ResponseBody
    public JsonResult updateUser(@RequestBody UpdateUserDTO updateUserDTO){
        //打桩
        log.debug(updateUserDTO.toString());
        //更新用户数据
        User user = new User();
        BeanUtils.copyProperties(updateUserDTO,user);
        user.setCreated(new Date());
        userMapper.updateUser(user);
        return JsonResult.success();
    }

    /**
     * 5.查询指定用户信息功能[全字段]
     * 请求地址: /v1/user/用户id
     * 请求方法: GET
     * 请求参数: 无
     * 返回响应: User
     */
    @ApiOperation(value = "5.查询指定用户信息")
    @ApiImplicitParam(name = "id",value = "用户编号",required = true,
            example = "1",dataType = "int")
    @GetMapping("/{id}")
    public JsonResult selectUserById(@PathVariable int id){
        User user = userMapper.selectUserById(id);
        return JsonResult.success(user);
    }

}
